package org.zjx.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zjx.common.bean.Result;
import org.zjx.common.route.IRoute;
import org.zjx.common.route.RouteManager;
import org.zjx.common.route.bean.Address;
import org.zjx.dao.dto.WayPointsDTO;
import org.zjx.dao.entity.Waypoint;
import org.zjx.dao.vo.WayPointsVO;
import org.zjx.service.IWaypointService;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-05-29
 */
@Tag(name = "传送点管理接口")
@RestController
@RequestMapping("/waypoint")
@RequiredArgsConstructor
public class WaypointController {
    private final IWaypointService waypointService;

    @Operation(summary = "上传传送点")
    @PostMapping("/saveWaypoint")
    public Result saveWaypoint(@RequestBody Waypoint waypoint) {

        waypointService.save(waypoint);
        return Result.isOk();
    }

    @Operation(summary = "寻路算法")
    @PostMapping("/findWay")
    public Result<WayPointsVO> findWay(@RequestBody WayPointsDTO wayPointsDTO) {
        if (wayPointsDTO.getStartAddress() == null || wayPointsDTO.getEndAddress() == null){
            return Result.isFail().msg(new RuntimeException("参数错误"));
        }

        List<IRoute> routes = waypointService.routes(wayPointsDTO.getPath(), wayPointsDTO.getStartAddress(), wayPointsDTO.getEndAddress());

        if (routes == null || routes.isEmpty()){
            return Result.isFail().msg(new Exception("路径不通"));
        }else {
            WayPointsVO wayPointsVO = new WayPointsVO();
            wayPointsVO.setPath(routes);
            return Result.isOk().data(wayPointsVO);
        }
    }
}